package com.example.latte_core.app;

import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.WeakHashMap;

public class Configurator {

    private static  final HashMap<String,Object> LATTE_COMFIGS  = new HashMap<>();
    private static  final ArrayList<IconFontDescriptor> ICONS = new ArrayList<>();


    private Configurator(){
        LATTE_COMFIGS.put(ConfigType.CONFIG_READ.name(),false);
    }

    static  Configurator getInstance(){
        return  Holder.INSTANCE;
    }

    final HashMap<String,Object> getLatteComfigs(){
        return LATTE_COMFIGS;
    }

    private static  class Holder{
        private static  final  Configurator INSTANCE = new Configurator();

    }

    public  final  void configure(){
        initIcons();
        LATTE_COMFIGS.put(ConfigType.CONFIG_READ.name(), true);
    }

    public final  Configurator withApiHost(String host){
        LATTE_COMFIGS.put(ConfigType.API_HOST.name(), host);
        return this;
    }
    private void  initIcons(){
        if(ICONS.size()>0){
            final Iconify.IconifyInitializer initializer = Iconify.with(ICONS.get(0));
            for(int i =1;i<ICONS.size();i++){
                initializer.with(ICONS.get(i));
            }
        }
    }
    public final  Configurator withIcon(IconFontDescriptor descriptor){
        ICONS.add(descriptor);
        return  this;
    }

      private void checkConfiguratition(){
        final  boolean isReady = (boolean) LATTE_COMFIGS.get(ConfigType.CONFIG_READ.name());
        if(!isReady){
            throw new RuntimeException("Configuration is not read,call configure");
        }
      }

      @SuppressWarnings("unchecked")
      final <T> T getConfiguration(Enum<ConfigType> key){
          checkConfiguratition();
          return (T) LATTE_COMFIGS.get(key);
      }
}
